package com.systemic.service.impl;

import com.systemic.mapper.FriendApplyMapper;
import com.systemic.mapper.FriendMapper;
import com.systemic.mapper.UserInfoMapper;
import com.systemic.pojo.Friend;
import com.systemic.pojo.FriendApply;
import com.systemic.pojo.UserInfo;
import com.systemic.service.FriendService;
import com.systemic.vo.Contact;
import com.systemic.vo.FriendInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: FriendServiceImpl
 * @Description: 好友模块实现类
 * @Author:
 * @CreateTime: 2023-12-30 15:41
 * @Version:
 **/
@Service
public class FriendServiceImpl implements FriendService {

    @Resource
    private FriendMapper friendMapper;

    @Resource
    private UserInfoMapper userInfoMapper;

    @Resource
    private FriendApplyMapper friendApplyMapper;


    /**
     * 好友列表
     * @param uid
     * @return
     */
    @Override
    @Transactional
    public List<Contact> selectListById(Integer uid) {
        // 查询friend表 uid、fid、nickname
        List<Friend> friends = friendMapper.selectListById(uid);
        if (friends!=null){
            List<Contact> contactList = new ArrayList<>();
            for (Friend friend: friends) {
                Contact contact = new Contact();
                UserInfo userInfo = new UserInfo();
                // 根据friend表查询的fid查询userInfo的avatar
                int fid = friend.getFid();
                userInfo = userInfoMapper.selectUserInfoByUid(fid);
                try {
                   contact.setUid(friend.getUid());
                   contact.setFid(fid);
                   contact.setNickname(friend.getNickname());
                   if (userInfo!=null){
                       contact.setAvatar(userInfo.getAvatar());
                   }
                   contactList.add(contact);
                } catch (Exception e){
                   throw new RuntimeException("错误"+e);
                }
            }
            return contactList;
        }
        return null;
    }

    /**
     * 查看好友详细信息
     * @param fid
     * @return
     */
    @Override
    @Transactional
    public FriendInfo selectFriendInfoByFid(Integer fid) {
        UserInfo userInfo = userInfoMapper.selectUserInfoByUid(fid);
        FriendInfo friendInfo = new FriendInfo();
        if (userInfo!=null){
            friendInfo.setFid(userInfo.getUid());
            friendInfo.setUname(userInfo.getUname());
            friendInfo.setArea(userInfo.getArea());
            friendInfo.setBirthday(userInfo.getBirthday());
            friendInfo.setEmail(userInfo.getEmail());
            friendInfo.setAvatar(userInfo.getAvatar());
            friendInfo.setBackground(userInfo.getBackground());
        } else{
            throw new RuntimeException("用户信息为空");
        }
        return friendInfo;
    }

    /**
     * 好友列表（根据昵称模糊查询）
     * @param nickname
     * @return
     */
    @Override
    @Transactional
    public List<Contact> selectListByName(String nickname) {
        List<Friend> friends = friendMapper.likeSelectList(nickname);
        List<Contact> contactList = new ArrayList<>();
        if (friends!=null){
            try{
                for (Friend friend:friends) {
                    int fid = friend.getFid();
                    UserInfo userInfo = userInfoMapper.selectUserInfoByUid(fid);
                    Contact contact = new Contact();
                    contact.setUid(friend.getUid());
                    contact.setFid(fid);
                    if (userInfo!=null){
                        contact.setAvatar(userInfo.getAvatar());
                    }
                    contact.setNickname(friend.getNickname());
                    contactList.add(contact);
                }
            } catch(Exception e){
                throw new RuntimeException("出现错误" +e);
            }
            return contactList;
        }
        return null;
    }

    /**
     * 修改好友昵称
     * @param friend
     * @return
     */
    @Override
    @Transactional
    public int updateNickname(Friend friend) {
        int row = 0;
        if (friend.getNickname()!=null){
            row = friendMapper.updateNickname(friend);
            if (row==0){
                throw new RuntimeException("修改失败");
            }
        }
        return row;
    }

    /**
     * 删除好友
     * @param friend
     * @return
     */
    @Override
    @Transactional
    public int delete(Friend friend) {
        // 先删除 好友申请表 关于uid和target的字段
        if (friend!=null){
            FriendApply apply1 = new FriendApply();
            FriendApply apply2 = new FriendApply();
            int user1 = friend.getUid();
            int user2 = friend.getFid();

            apply1.setUid(user1);
            apply1.setTarget(user2);
            int row1 = friendApplyMapper.delete(apply1);

            apply2.setUid(user2);
            apply2.setTarget(user1);
            int row2 = friendApplyMapper.delete(apply2);

            // 再删除 好友表 关于uid和fid的字段
            int row3 = friendMapper.delete(friend);
            Friend friend1 = new Friend();
            friend1.setUid(user2);
            friend1.setFid(user1);
            int row4 = friendMapper.delete(friend1);

            int row = row1 + row2 + row3 + row4;
            if (row==0){
                throw new RuntimeException("当前没有删除的");
            }
            return row;
        }
        return 0;
    }


}
